package com.nvc.homeplan.data;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import android.R;

import com.nvc.homeplan.Constants;
import com.nvc.homeplan.ContentDownloader;
import com.nvc.homeplan.data.vo.*;

public class Plan 
{
    public ArrayList<PlanVO> getPlans(String searchType, String searchUrlQueryString) 
    {
        long startTime = System.currentTimeMillis();
        ArrayList<PlanVO> results = new ArrayList<PlanVO>();

        try {
        	
    		ContentDownloader contentDownloader = new ContentDownloader();
    		String url = Constants.baseDataUrl + "mobileSearch.cfm?" + searchUrlQueryString;
    		String result = contentDownloader.downloadContent(url);
    		
    		JSONObject jobject = new JSONObject(result);
    		JSONArray jArray = (JSONArray) jobject.get("DATA");
    		
    		results = new ArrayList<PlanVO>();
    		for (int ctr=0; ctr < jArray.length(); ctr++ )
    		{
    			JSONArray jsonData = (JSONArray) jArray.get(ctr);
    			PlanVO planVO = new PlanVO();
    			/*
    			planVO.id = jsonData.getInt("id");
    			planVO.finishedsf = jsonData.getInt("finishedsf");
    			planVO.noOfBath = jsonData.getInt("bath");
    			planVO.noOfBed = jsonData.getInt("bed");
    			planVO.dimension = jsonData.getString("dimension");
    			planVO.imageURL = jsonData.getString("image");
    			*/
    			planVO.id = jsonData.getInt(1);
    			planVO.finishedsf = jsonData.getInt(2);
    			planVO.noOfBath = jsonData.getInt(4);
    			planVO.noOfBed = jsonData.getInt(3);
    			planVO.dimension = jsonData.getInt(5) + "' Wide x " + jsonData.getInt(6) + "' Deep";
    			planVO.imageURL =  Constants.baseImageUrl  + "/plans/" + jsonData.getInt(1) + "/" + jsonData.getInt(1) + "-Bth.jpg";
    			planVO.plantypeName = jsonData.getString(7);
    			planVO.price  = jsonData.getDouble(8);
    			if (planVO.plantypeName.equalsIgnoreCase("project plan"))
    			{
    				planVO.type = "PP";
    			}
    			if (planVO.plantypeName.equalsIgnoreCase("plan book"))
    			{
    				planVO.type = "PB";
        			planVO.imageURL =  Constants.baseImageUrl  + "/planbooks/" + jsonData.getInt(1) + ".jpg";
    			}

    			planVO.retailPrice =   jsonData.getDouble(9);
    			if (jsonData.isNull(10))
    				planVO.freeShipping = false;
    			else
    			{
    				try {
    					planVO.freeShipping = jsonData.getBoolean(10);
    				} 
    				catch (Exception e)
    				{
    					if (jsonData.getInt(10) == 0)
    						planVO.freeShipping = false;
    					else
    						planVO.freeShipping = true;
    				}
    			}
    			planVO.priceBaseShipping= jsonData.getInt(11); 	
    			
    			
    			results.add(planVO);
    		}
        } catch (Exception e) {
            //Log.e(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG, e);
        }
        long duration = System.currentTimeMillis() - startTime;
        //Log.v(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG + " call and parse duration - " + duration);
        return results;
    }

    public PlanVO getSinglePlan(int planID) 
    {
		//JSONObject jsonData = new JSONObject(result);

        long startTime = System.currentTimeMillis();
        ArrayList<PlanVO> results = null;

        /*
        try {
        	
    		ContentDownloader contentDownloader = new ContentDownloader();
    		//String result = contentDownloader.downloadContent("http://192.168.1.103/android/plans.txt");
    		String result = contentDownloader.downloadContent("http://10.31.70.223/p/plans.txt");
    		JSONArray jArray = new JSONArray(result);
    		
    		results = new ArrayList<PlanVO>();
    		for (int ctr=0; ctr < jArray.length(); ctr++ )
    		{
    			JSONObject jsonData = jArray.getJSONObject(ctr);
    			PlanVO planVO = new PlanVO();
    			planVO.id = jsonData.getInt("id");
    			planVO.finishedsf = jsonData.getInt("finishedsf");
    			planVO.noOfBath = jsonData.getInt("bath");
    			planVO.noOfBed = jsonData.getInt("bed");
    			planVO.dimension = jsonData.getString("dimension");
    			planVO.imageURL = jsonData.getString("image");
    			results.add(planVO);
    			
    			if (planVO.id == planID)
    			{
    				return planVO;
    			}
    		}
        } catch (Exception e) {
            //Log.e(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG, e);
        }
        */
        
        PlanVO planVO = new PlanVO();
        planVO.imageURL = "http://192.168.1.103/android/test.htm";
        
        long duration = System.currentTimeMillis() - startTime;
        //Log.v(Constants.LOGTAG, " " + ReviewFetcher.CLASSTAG + " call and parse duration - " + duration);
        
        return null;
    }
    
}
